package com.zzq.my.jetpacks.jectpackapp.view

import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import android.widget.ListView

/**
 * 描述：class describe
 * 作者：zhuangzeqin
 * 时间: 2022/11/9-15:48
 * 邮箱：zzq@eeepay.cn
 * 备注:
 */
class FixListView : ListView {
    //多个构造器1
    constructor(ctx:Context) : super(ctx)
    //多个构造器2
    constructor(ctx: Context,attr: AttributeSet):super(ctx,attr)

    private var mLastX = 0
    private var mLastY = 0


    override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
        val x = ev!!.x.toInt()
        val y = ev!!.y.toInt()
        val action = ev!!.action and MotionEvent.ACTION_MASK
        when (action) {
            MotionEvent.ACTION_DOWN -> parent.requestDisallowInterceptTouchEvent(true)
            MotionEvent.ACTION_MOVE -> {
                //水平移动的增量
                val deltaX = x - mLastX
                //竖直移动的增量
                val deltaY = y - mLastY
                //当水平增量大于竖直增量时，表示水平滑动，此时需要父View去处理事件
                // Math.abs(dX) > Math.abs(dY) >>> 左右滑动
                // Math.abs(dX) < Math.abs(dY) >>> 上下滑动
                if (Math.abs(deltaX) > Math.abs(deltaY)) {
                    parent.requestDisallowInterceptTouchEvent(false)
                }
            }
            MotionEvent.ACTION_UP -> {}
            else -> {}
        }
        mLastX = x
        mLastY = y
        return super.dispatchTouchEvent(ev)
    }
}